import 'package:after_sale_plugin/r.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:lx_widgets_library/enum/order_state_enum.dart';
import 'package:lx_widgets_library/extension/lx_colors.dart';
import 'package:lx_widgets_library/language/language_util.dart';
import 'package:lx_widgets_library/lx_widgets_library.dart';

// 工单是否解决的状态
enum ProblemResolvedState {
  // 未选择
  unselected,
  // 已解决
  resolved,
  // 未解决
  unsolved,
  // 不存在‘评价’功能
  none,
}

class ProblemContenView extends StatelessWidget {
  // 工单进度状态
  final Rx<OrderProgressState> orderProgressState;

  final String title;

  // 问题描述的内容
  final RxString problemDescContent;

  // 解决状态
  final Rx<ProblemResolvedState> problemResolvedState;

  // ‘解决’/‘未解决’事件
  final Function(ProblemResolvedState)? feedbackOnTap;

  ProblemContenView({
    required this.title,
    required this.orderProgressState,
    required this.problemDescContent,
    required this.problemResolvedState,
    this.feedbackOnTap,
  }) {
    lineCount = _calculateLineCount(problemDescContent.value);
  }

  int lineCount = 0;
  int maxLine = 2;

  RxBool isOpen = false.obs;

  @override
  Widget build(BuildContext context) {
    return Obx(
      () => Padding(
        padding: EdgeInsets.only(
          left: CONTENT_MARGIN,
          right: CONTENT_MARGIN,
          top: CONTENT_MARGIN,
          bottom: CONTENT_MARGIN / 2,
        ),
        child: ClipRRect(
          borderRadius: const BorderRadius.all(Radius.circular(8)),
          child: Container(
            padding: EdgeInsets.all(CONTENT_MARGIN),
            color: LxColors.borderBackgroud,
            width: double.infinity,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    LxText(
                      title,
                      color: Colors.white.withOpacity(0.6),
                      fontWeightEnum: LxFontWeightEnum.bodyMedium,
                    ),
                    Visibility(
                      visible: orderProgressState.value ==
                              OrderProgressState.closed &&
                          problemResolvedState.value !=
                              ProblemResolvedState.none,
                      child: _selectBtn(
                        true,
                        AppLanguageUtil.to.titleResolved,
                        true,
                        () {},
                        isStatus: true,
                      ),
                    ),
                  ],
                ),
                SizedBox(height: CONTENT_MARGIN),
                ExpandableText(
                  text: problemDescContent.value,
                  maxLines: 2,
                  textStyle: _textStyle(),
                  linkTextExpand: " ${AppLanguageUtil.to.titleExpand}",
                  linkTextCollapse: " ${AppLanguageUtil.to.titleCollaspe}",
                  linkTextColor: LxColors.theme,
                ),
                Visibility(
                  visible: orderProgressState.value ==
                          OrderProgressState.completed &&
                      problemResolvedState.value != ProblemResolvedState.none,
                  child: Column(
                    children: [
                      SizedBox(height: CONTENT_MARGIN),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.end,
                        children: [
                          _selectBtn(
                            true,
                            AppLanguageUtil.to.titleResolved,
                            true,
                            () {
                              if (feedbackOnTap != null) {
                                feedbackOnTap!(ProblemResolvedState.resolved);
                              }
                            },
                          ),
                          SizedBox(width: CONTENT_MARGIN),
                          _selectBtn(
                            false,
                            AppLanguageUtil.to.titleUnsolved,
                            problemResolvedState.value ==
                                ProblemResolvedState.unsolved,
                            () {
                              if (feedbackOnTap != null) {
                                feedbackOnTap!(ProblemResolvedState.unsolved);
                              }
                            },
                          ),
                        ],
                      )
                    ],
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }

  Widget _selectBtn(
    bool isResolved,
    String title,
    bool isSelected,
    Function onTap, {
    bool isStatus = false,
  }) {
    return InkWell(
      onTap: () {
        onTap();
      },
      child: Container(
        width: 88,
        height: 28,
        decoration: isStatus
            ? null
            : BoxDecoration(
                borderRadius: const BorderRadius.all(
                  Radius.circular(14),
                ),
                border: Border.all(
                  width: 1.0,
                  color: isSelected ? LxColors.theme : const Color(0xFF5E5E5E),
                ),
              ),
        child: Center(
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              LxColorUtil.imageFilter(
                Image.asset(
                  isResolved ? R.good_png : R.poor_png,
                  package: PACKAGE_NAME,
                  width: 17,
                  fit: BoxFit.fitWidth,
                ),
                isSelected ? LxColors.theme : Colors.white,
              ),
              const SizedBox(width: 3),
              LxText(
                title,
                color: isSelected ? LxColors.theme : Colors.white,
                fontWeightEnum: LxFontWeightEnum.bodySmall,
              )
            ],
          ),
        ),
      ),
    );
  }

  // 计算文本行数
  int _calculateLineCount(String text) {
    final textPainter = TextPainter(
      text: TextSpan(text: text, style: _textStyle()),
      maxLines: null,
      textDirection: TextDirection.ltr,
    );

    textPainter.layout(minWidth: 0, maxWidth: screenWidth - 4 * CONTENT_MARGIN);

    return textPainter.computeLineMetrics().length;
  }

  // 文本样式
  TextStyle _textStyle() {
    return const TextStyle(
      color: Colors.white,
      fontWeight: FontWeight.w400,
      fontSize: 14,
    );
  }
}
